package com.project.platformcommon.email;

import com.project.platformcommon.Bean.EmailBean;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;

import javax.mail.internet.MimeMessage;

@Service("sendEmail")
@EnableAsync  //开始异步支持
public class SendEmail {

    Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);

//    @Value("${com.example.mail.sendNumber}")
//    private Integer sendNumber;

    @Autowired
    private JavaMailSender javaMailSender;

    public void sendVerCodeMail(EmailBean mailBean) {
        MimeMessage mimeMailMessage = null;
        try {
            mimeMailMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
            mimeMessageHelper.setFrom(mailBean.getFrom());
            mimeMessageHelper.setTo(mailBean.getRecipient());
            mimeMessageHelper.setSubject("platform平台注册验证码");
            StringBuilder sb = new StringBuilder();
            sb.append("<h1>SpirngBoot测试邮件HTML</h1>")
                    .append("您的校验码为：<B style='color:#F00'>"+mailBean.getVerCode()+"</B>")
                    .append("<p style='text-align:right'>右对齐</p>");
            mimeMessageHelper.setText(sb.toString(), true);
            javaMailSender.send(mimeMailMessage);
        } catch (Exception e) {
            logger.error("邮件发送失败", e.getMessage());
        }
    }
}
